package com.example.yaja.yaja.controller;

import com.example.yaja.yaja.entity.Activity;
import com.example.yaja.yaja.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.DateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Zero on 2017/12/4.
 */
@RestController
@RequestMapping("/activity")
public class ActivityController {


    @Autowired
    ActivityService activityService;


    /**
     * 查找活动信息
     * @return
     */
    @RequestMapping(value = "/find",method = RequestMethod.GET)
    public Map<String,Object> findActivities(){

        Map<String,Object> map = new HashMap<String, Object>();
        List<Activity> activities=activityService.findActivityByNumber(4);
        if(activities!=null){
            for(int i=0;i<activities.size();i++){
                String time= DateFormat.getDateInstance().format(activities.get(i).getTime());
                activities.get(i).setTimeA(time);
            }
            map.put("activities",activities);
        }else {
            map.put("activityState",0);
        }
        return map;
    }


    /**
     * 查找所有活动信息
     * @return
     */
    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    public Map<String,Object> findAllActivities(){

        Map<String,Object> map = new HashMap<String, Object>();
        List<Activity> activities=activityService.findAllActivity();
        if(activities!=null){
            for(int i=0;i<activities.size();i++){
                String time= DateFormat.getDateInstance().format(activities.get(i).getTime());
                activities.get(i).setTimeA(time);
            }
            map.put("activities",activities);
        }else {
            map.put("activityState",0);
        }
        return map;
    }
}
